package cn.zhiran.org.controller;

import cn.zhiran.basic.util.AjaxResult;
import cn.zhiran.basic.util.PageList;
import cn.zhiran.org.domain.Employee;
import cn.zhiran.org.query.EmployeeQuery;
import cn.zhiran.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询一条
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "员工查询",notes = "查询一条，需要参数id")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "员工查询",notes = "查询所有，不需要参数")
    public List<Employee> queryAll(){
        return employeeService.loadAll();
    }

    /**
     * 删除一条
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除",notes = "根据id删除，参数id")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除",notes = "根据id删除，参数id")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }




    /**
     * 添加或修改
     * 对象接收的前端发送的请求携带：
     * @param employee
     * @return
     */
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    /**
     * 分页+高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页和高级查询",notes = "分页携带参数currentPage，pageSize")
    public PageList<Employee> queryData(@RequestBody EmployeeQuery query){
        try {
            return employeeService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

}
